<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <% String path = request.getContextPath();%>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="<%=path%>/layui/css/layui.css">
    <script src="<%=path%>/jquery-3.4.1.js"></script>
    <script src="<%=path%>/layui/layui.js" type="text/javascript"></script>
</head>
<body onload="init();">
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-form">
            <form class="layui-form" style="margin-top: 8%;">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userID" disabled="disabled" placeholder="用户ID" class="layui-input" id="input_userID">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" disabled="disabled" placeholder="用户姓名" class="layui-input" id="input_userName">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户角色</label>
                    <div class="layui-input-inline">
                        <input type="text" name="character" disabled="disabled" placeholder="用户角色" class="layui-input" id="input_character">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">账号密码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="password" oninput="value=value.replace(/[^\d]/g,'')" placeholder="账号密码" class="layui-input" id="input_password">
                    </div>
                    <div style="color: red; font-size: larger;">*</div>
                </div>
                </br>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button id="btn0" class="layui-btn layui-btn-normal" onclick="updatePsw();return false;"lay-filter="due1">更新</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</div>
<script type="text/javascript">
    function init() {
        parent_json=eval('('+parent.json+')');
        $('#input_userID').val(parent_json.userCode);
        $('#input_userName').val(parent_json.userName);
        $('#input_character').val(parent_json.userRole);
        $('#input_password').val(parent_json.userPassWord);
    }
    function updatePsw(){
        let newPwd=$('#input_password').val();
        if (newPwd.length<8 || newPwd.length>16){
            layer.tips('密码长度应保持在8~16位，请重新设置', '#input_password', {tips: [1, '#009688'], time: 3000});
        }else {
            $.ajax({
                type:"POST"
                ,dataType:"json"
                ,url:"${pageContext.request.contextPath}/jsp/root.do?method=modify&userCode="+eval('('+parent.json+')').userCode+"&pwd="+newPwd
                ,success:function (data) {
                    if (data.ok === 'yes'){
                        layer.msg('更新成功',{time:2000},function () {
                            eval('('+parent.json+')').userPassWord=newPwd;
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.render();
                            parent.layer.close(index);
                        });
                    }else if (data.ok === 'no'){
                        layer.msg('更新失败');
                    }
                }
                ,error:function () {
                    layer.msg('连接异常，更新失败');
                }
            })
        }
    }
</script>
</body>
</html>